package academic.ui.client.view;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasText;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.LoginPresenter;
import academic.ui.client.widgets.forms.ForgotPasswordForm;
import academic.ui.client.widgets.forms.RegistrationForm;
import academic.ui.client.widgets.forms.SignInForm;
import ecommerce.soco.shared.GWT_Customer;

public class LoginView extends ViewImpl implements LoginPresenter.MyView {

	public GenericContainerTag div = new GenericContainerTag("div");
	
	
	public RegistrationForm registrationForm = new RegistrationForm();
	public SignInForm signInForm = new SignInForm();
	private ForgotPasswordForm forgotPassForm = new ForgotPasswordForm();
	
	public Button signInButton = new Button();
	public Button registerButton = new Button();
	private Button forgotPassButton = new Button();
	private GenericContainerTag submitProfileSync = new GenericContainerTag("span","submit");
	private GenericClickable submitButton = new GenericClickable("a");
	
	public GenericContainerTag slot = new GenericContainerTag("div");
	public GenericClickable forgotPasswd = new GenericClickable("a","Forgot Your Password?");
	public GenericClickable register = new GenericClickable("a","Sign Up");
	public GenericClickable signInLink = new GenericClickable("a","Sign In");
	public GenericClickable cancelForgotPassLink = new GenericClickable("a","cancel");
	
	public GenericContainerTag registerDiv = new GenericContainerTag("div");
	public GenericContainerTag alreadyRegisteredDiv = new GenericContainerTag("div");
	private GenericContainerTag forgotPasswdDiv = new GenericContainerTag("div");
	private GenericContainerTag postForgotPassDiv = new GenericContainerTag("div");
	
	private KeyDownHandler submitFormHandler = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER) {
				NativeEvent e = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
				DomEvent.fireNativeEvent(e, signInButton);
			}
		}
		
	};
	
	private GenericContainerTag loginBody = new GenericContainerTag("div");
	
	@Inject
	public LoginView(CSSConstants css) {
		super();
		div.setStyleName("login");
		signInForm.getpasswdKeyDown().addKeyDownHandler(submitFormHandler);
		signInForm.getEmailKeyDown().addKeyDownHandler(submitFormHandler);
		signInButton.setStyleName("signInLink");
		registerButton.setStyleName("signUpLink");
		forgotPassButton.setStyleName("forgotPassButton");
		submitProfileSync.setStyleName("submitButton");
		submitButton.setStyleName("submitButtonLink");
		
		GenericContainerTag h3EmailInstr = new GenericContainerTag("h3","Check Your Email");
		GenericContainerTag checkEmailInstr = new GenericContainerTag("p","One more quick step! " +
				"Check your email for a note from Groupon, and click the link " +
				"inside to reset your password.");
		GenericContainerTag moreInstr = new GenericContainerTag("p","Didn't get your password reset " +
				"email? Be sure to check your spam folder and add mail@mysocodeals.com " +
				"to your address book to ensure delivery. If you still haven't received " +
				"your activation email, enter your email below and we'll send you another.");
		postForgotPassDiv.setStyleName("twoSixtyWidth");
		postForgotPassDiv.add(h3EmailInstr);
		postForgotPassDiv.add(checkEmailInstr);
		postForgotPassDiv.add(moreInstr);
		div.add(loginBody);
	}
	
	@Override
	public Widget asWidget() {
		return div;
	}

	@Override
	public void setup() {
		loginBody.clear();
		loginBody.setStyleName("loginBody");
		registerDiv.clear();
		alreadyRegisteredDiv.clear();
		
		updateForSignIn();
		
		GenericContainerTag notregistered = new GenericContainerTag("span","Not Registered? ");
		GenericContainerTag alreadyRegistered = new GenericContainerTag("span","Already have an account? ");
		registerDiv.add(notregistered);
		registerDiv.add(register);
		loginBody.add(registerDiv);
		loginBody.add(alreadyRegisteredDiv);
		alreadyRegisteredDiv.add(alreadyRegistered);
		alreadyRegisteredDiv.add(signInLink);
		
		
		//add both forms
		signInForm.reset();
		loginBody.add(signInForm);
		loginBody.add(registrationForm);
		loginBody.add(forgotPasswdDiv);
		loginBody.add(postForgotPassDiv);
		forgotPasswdDiv.add(forgotPassForm);
		forgotPasswdDiv.add(forgotPassButton);
		forgotPasswdDiv.add(cancelForgotPassLink);
		loginBody.add(signInButton);
		loginBody.add(registerButton);
		GenericContainerTag forgotPasswdDiv = new GenericContainerTag("div");
		forgotPasswdDiv.add(forgotPasswd);
		loginBody.add(forgotPasswdDiv);
	}
	

	@Override
	public HasClickHandlers getSignIn() {
		return signInButton;
	}
	
	@Override
	public HasClickHandlers getRegisterButton() {
		return registerButton;
	}
	
	@Override
	public HasText getLoginEmail() {
		return signInForm.getEmail();
	}
	
	@Override
	public HasText getLoginPasswd() {
		return signInForm.getPasswd();
	}
	
	@Override
	public HasClickHandlers register() {
		return register;
	}

	
	@Override
	public void updateForRegistration() {
		removeErrors();
		registerDiv.setVisible(false);
		alreadyRegisteredDiv.setVisible(true);
		signInForm.setVisible(false);
		registrationForm.setVisible(true);
		registerButton.setVisible(true);
		signInButton.setVisible(false);
		forgotPasswd.setVisible(false);
		forgotPasswdDiv.setVisible(false);
		postForgotPassDiv.setVisible(false);
	}
	
	@Override
	public HasClickHandlers getSignInLink() {
		return signInLink;
	}

	/**
	 * Returns false if there are any errors
	 */
	@Override
	public Boolean checkSignInRules() {
		return signInForm.checkRules();
	}

	@Override
	public void setLoginError(String error) {
		signInForm.setError(error);
	}

	@Override
	public void updateForSignIn() {
		removeErrors();
		registerDiv.setVisible(true);
		alreadyRegisteredDiv.setVisible(false);
		signInForm.setVisible(true);
		registrationForm.setVisible(false);
		signInButton.setVisible(true);
		registerButton.setVisible(false);
		forgotPasswd.setVisible(true);
		forgotPasswdDiv.setVisible(false);
		postForgotPassDiv.setVisible(false);
	}

	private void removeErrors() {
		registrationForm.removeErrors();
		signInForm.removeErrors();
		forgotPassForm.removeErrors();
	}

	@Override
	public boolean checkRegistrationRules() {
		return registrationForm.checkRules();
	}
	
	@Override
	public boolean checkForgotPasswordRules() {
		return forgotPassForm.checkRules();
	}
	@Override
	public HasText getRecoverPassEmail() {
		return forgotPassForm.getEmail();
	}

	@Override
	public GWT_Customer getCustomer() {
		return registrationForm.getCustomer();
	}
	
	@Override
	public HasClickHandlers forgotPasswd() {
		return forgotPasswd;
	}

	@Override
	public void updateForForgotPassword() {
		removeErrors();
		registerDiv.setVisible(false);
		signInForm.setVisible(false);
		signInButton.setVisible(false);
		forgotPasswd.setVisible(false);
		forgotPasswdDiv.setVisible(true);
		postForgotPassDiv.setVisible(false);
	}
	
	@Override
	public HasClickHandlers cancelForgotPass() {
		return cancelForgotPassLink;
	}
	
	@Override
	public HasClickHandlers forgotPassButton() {
		return forgotPassButton;
	}

	@Override
	public void updatePostForgotPass() {
		postForgotPassDiv.setVisible(true);
		forgotPasswdDiv.setVisible(false);
	}

	private GenericClickable cancelSyncLink = new GenericClickable("a","cancel");
	
	@Override
	public void setupLoginFromFB(String email) {
		GenericContainerTag fbInformational = new GenericContainerTag("div","You already have a socodeals account!");
		
		GenericContainerTag fbInformational2 = new GenericContainerTag("p","Enter your password to link Facebook Connect with your existing socodeals account");
		
		loginBody.clear();
		loginBody.setStyleName("fbLoginBody");
		loginBody.add(fbInformational);
		loginBody.add(fbInformational2);
		//add both forms
		loginBody.add(signInForm);
		signInForm.setEmail(email);
		submitButton.add(submitProfileSync);
		loginBody.add(submitButton);
		loginBody.add(cancelSyncLink);
	}
	
	@Override
	public HasClickHandlers cancelSyncLink() {
		return cancelSyncLink;
	}
	@Override
	public HasClickHandlers submitSyncProfile() {
		return submitButton;
	}

}
